package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.MyCategoryStaffDto;
import zhihuilan.dto.MyStaffShopDto;
import zhihuilan.dto.MyStaffShopGoodsDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyCategoryStaff;
import zhihuilan.mapper.MyCategoryStaffMapper;
import zhihuilan.params.query.MyCategoryStaffQueryParam;
import zhihuilan.params.save.MyCategoryStaffSaveParam;
import zhihuilan.service.MyCategoryStaffService;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyCategoryStaffServiceImpl extends ServiceImpl<MyCategoryStaffMapper, MyCategoryStaff> implements MyCategoryStaffService {

    @Override
    public WxMiniPageDto<MyCategoryStaffDto> selectPageList(MyCategoryStaffQueryParam queryParam) {
        QueryWrapper<MyCategoryStaff> queryWrapper = new QueryWrapper<>();

        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
                queryWrapper.eq("pid", 0);
            }
            if (!ObjectUtils.isEmpty(queryParam.getShopId())) {
                queryWrapper.eq("pid", queryParam.getShopId());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
                queryWrapper.eq("status", queryParam.getStatus());
            } else {
                queryWrapper.eq("status", 1);
            }
        }
        queryWrapper.orderByAsc("sort");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyCategoryStaff> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyCategoryStaffDto> result = new WxMiniPageDto(page.getCurrent(), page.getSize());
        BeanUtils.copyProperties(page, result);
        List<MyCategoryStaffDto> convertList = page.getRecords().stream().map(po -> {
            MyCategoryStaffDto dto = new MyCategoryStaffDto();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
        result.setData(convertList);
        return result;
    }

    @Override
    public List<MyCategoryStaffDto> selectList(MyCategoryStaffQueryParam queryParam) {
        QueryWrapper<MyCategoryStaff> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
                queryWrapper.eq("pid", 0);
            }
            if (!ObjectUtils.isEmpty(queryParam.getShopId())) {
                queryWrapper.eq("pid", queryParam.getShopId());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
                queryWrapper.eq("status", queryParam.getStatus());
            } else {
                queryWrapper.eq("status", 1);
            }
        }
        queryWrapper.orderByAsc("sort");
        List<MyCategoryStaff> selectList = this.baseMapper.selectList(queryWrapper);
        return selectList.stream().map(po -> {
            MyCategoryStaffDto dto = new MyCategoryStaffDto();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public MyCategoryStaff selectById(Integer id) {
        MyCategoryStaff result = this.baseMapper.selectById(id);
        return result;
    }

    @Override
    public MyCategoryStaff addEntity(MyCategoryStaff entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public MyCategoryStaff updateEntity(MyCategoryStaff entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public Boolean deletetById(Integer id) {
        int result = this.baseMapper.deleteById(id);
        return result > 0 ? true : false;
    }

    @Override
    public Boolean updateEntitys(List<MyCategoryStaff> list){
        if (ObjectUtils.isEmpty(list)) {
            return false;
        }
        return this.updateBatchById(list);
    }
}
